//
//  XJ_AccountRechargeViewController.m
//  XunJieProject
//
//  Created by xue on 2018/6/11.
//  Copyright © 2018年 Soowin. All rights reserved.
//

#import "XJ_AccountRechargeViewController.h"
#import "XJ_ApplayResultViewController.h"
#import "XJ_NewCardViewController.h"
#import "XJ_ChangeBandsView.h"


@interface XJ_AccountRechargeViewController ()
@property (weak, nonatomic) IBOutlet UIButton *getCodeBtn;
@property (weak, nonatomic) IBOutlet UIButton *rechargeBtn;
@property (weak, nonatomic) IBOutlet UILabel *bandNameLabel;
@property (weak, nonatomic) IBOutlet ZDXXibTextField *cardIDTF;
@property (weak, nonatomic) IBOutlet UIImageView *logoImage;
@property (weak, nonatomic) IBOutlet ZDXXibTextField *phoneTF;
@property (weak, nonatomic) IBOutlet ZDXXibTextField *codeTF;
@property (weak, nonatomic) IBOutlet UITextField *chargeCountTF;
@property (weak, nonatomic) IBOutlet ZDXXibLabel *balanceLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *up_scrollView;

@property (nonatomic, strong) XJ_ChangeBandsView  *changeBandsView;
@end

@implementation XJ_AccountRechargeViewController

-(XJ_ChangeBandsView*)changeBandsView{
    
    if (!_changeBandsView) {
        _changeBandsView =[XJ_ChangeBandsView changeBandsView];
        _changeBandsView.frame =CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        [KWINDOW addSubview:_changeBandsView];
        UITapGestureRecognizer *myTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didTapBackgroundView:)];
        [_changeBandsView.contentView addGestureRecognizer:myTap];
    }
    return _changeBandsView;
}
- (void)didTapBackgroundView:(UITapGestureRecognizer *)sender {
    self.changeBandsView.hidden =YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    XJ_CardListModel *cardListModel =_cardListArray[0];
    self.bandNameLabel.text =cardListModel.bank;
    self.cardIDTF.text =cardListModel.cardNoTail;
    [self.logoImage sd_setImageWithURL:[NSURL URLWithString:cardListModel.bankLogo] placeholderImage:[UIImage imageNamed:@"img_no_image"]];
    self.balanceLabel.text =[NSString stringWithFormat:@"可用余额￥%@元",_userInfoModel.balance];
    
}
-(void)creatMainUI{
    self.nav_titleLabel.text =@"账户充值";
    _getCodeBtn.backgroundColor =Color_ZhuSeDiao;
    _getCodeBtn.layer.cornerRadius =4;
    _getCodeBtn.layer.masksToBounds =YES;
    _rechargeBtn.backgroundColor =Color_ZhuSeDiao;
    _rechargeBtn.layer.cornerRadius =4;
    _rechargeBtn.layer.masksToBounds =YES;
    _up_scrollView.constant =SafeAreaTopHeight;
    
    __weak typeof(self) weakSelf = self;
    self.changeBandsView.block = ^(NSInteger index, BOOL isNew) {
        
        if (isNew) {
            XJ_NewCardViewController *newCard =[[XJ_NewCardViewController alloc]init];
            [weakSelf.navigationController pushViewController:newCard animated:YES];
            
        }else{
            weakSelf.bandNameLabel.text =@"中国银行";
            
        }
    
    };
    self.changeBandsView.hidden =YES;
}
- (IBAction)Click_btn:(id)sender {
    
    UIButton *button =(UIButton*)sender;
    switch (button.tag) {
        case 0://更改充值卡
        {
            self.changeBandsView.hidden =NO;
            
        }
            break;
        case 1://获取验证码
        {
            if (_phoneTF.text.length==0) {
                ZDXAlertView *alext =[[ZDXAlertView alloc]initWithMessage:@"请输入手机号" image:@"pop_msg_tip_xs"];
                [alext showXLAlertView];
                return;
            }
            [self getCodeAction];
        }
            break;
        case 2://充值
        {
            if (_phoneTF.text.length==0) {
                ZDXAlertView *alext =[[ZDXAlertView alloc]initWithMessage:@"请输入手机号" image:@"pop_msg_tip_xs"];
                [alext showXLAlertView];
                return;
            }
            if (_codeTF.text.length==0) {
                ZDXAlertView *alext =[[ZDXAlertView alloc]initWithMessage:@"请输入验证码" image:@"pop_msg_tip_xs"];
                [alext showXLAlertView];
                return;
            }
            if (_chargeCountTF.text.length==0) {
                ZDXAlertView *alext =[[ZDXAlertView alloc]initWithMessage:@"请输入充值金额" image:@"pop_msg_tip_xs"];
                [alext showXLAlertView];
                return;
            }
            
            [self loadMoneyWithCharge];
      
        }
            break;
            
        default:
            break;
    }
}
#pragma mark--充值接口
-(void)loadMoneyWithCharge{
    
    NSUserDefaults * db = [NSUserDefaults standardUserDefaults];
    NSString * token = [db objectForKey:@"token"];
    
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    [dic setValue:IDENTIFIER forKey:@"equipmentNo"];
    [dic setValue:STSTERMVERSION forKey:@"systemVersion"];
    [dic setValue:APPVERSION forKey:@"appVersion"];
    [dic setValue:USERID forKey:@"userId"];
    [dic setValue:token forKey:@"token"];
    
    NSInteger chargeCount =[_chargeCountTF.text integerValue];
    XJ_CardListModel *cardListModel =_cardListArray[0];
    [dic setValue:cardListModel.cardId forKey:@"cardId"];
    [dic setValue:_phoneTF.text forKey:@"mobile"];
    [dic setValue:_codeTF.text forKey:@"verifyCode"];
    [dic setValue:@(chargeCount) forKey:@"chargeCount"];
    [XJ_Utils signPost:@"/money/charge?" parameters:dic success:^(NSInteger recode, id jsonObject) {
        
        
        //        NSDictionary * dataDic = [jsonObject objectForKey:@"data"];
        
        XJ_ApplayResultViewController *result =[[XJ_ApplayResultViewController alloc]init];
        result.resultType =@"1";
        result.ValueStr =self.chargeCountTF.text;
        [self.navigationController pushViewController:result animated:YES];
        
        
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
}
#pragma mark--登录后验证码发送接口
- (void)getCodeAction{
    
    NSUserDefaults * db = [NSUserDefaults standardUserDefaults];
    NSString * token = [db objectForKey:@"token"];
    
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    [dic setValue:IDENTIFIER forKey:@"equipmentNo"];
    [dic setValue:APPVERSION forKey:@"appVersion"];
    [dic setValue:STSTERMVERSION forKey:@"systemVersion"];
    [dic setValue:USERID forKey:@"userId"];
    
    if (token!=nil &&![token isEqualToString:@""]) {
        
        [dic setValue:token forKey:@"token"];
        
    }
    [dic setValue:_phoneTF.text forKey:@"mobile"];
    [dic setValue:@"5" forKey:@"verifyCodeType"];//3:实名认证短信发送验证码 4:添加信用卡短信发送验证码5:充值短信发送验证码 6:绑定信用卡并充值
    
    [XJ_Utils signPost:@"user/afterSendVerify?" parameters:dic success:^(NSInteger recode, id jsonObject) {
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
